<!DOCTYPE html>
<!--
This file is part of Cockpit.

Copyright (C) 2015 Red Hat, Inc.

Cockpit is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

Cockpit is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
-->
<html>

<head>
    <title>OpenConnect VPN</title>
    <meta charset="utf-8">
    <link href="./patternfly.css" type="text/css" rel="stylesheet">
    <link href="ocserv.css" type="text/css" rel="stylesheet">
    <script type="text/javascript" src="../base1/cockpit.js"></script>
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
</head>
<body hidden>
    <div class="pf-c-page">
        <main class="pf-c-page__main" tabindex="-1">
            <div class="curtain-ocserv blank-slate-pf" hidden>
              <h1>Couldn't connect to OpenConnect Server</h1>
              <p></p>
            </div>

            <section id="status-info" class="pf-c-page__main-section pf-m-light">
                <div class="panel panel-default">
                    <div class="panel-heading">
                       <span id="head">Server status</span>
                    </div>
                    <table id="status-table">
                    </table>
                </div>
            </section>

            <section id="users-info" class="pf-c-page__main-section pf-m-light">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <span id="head">Connected users</span>
                    </div>
                    <table class="table table-hover" id="users-table">
                        <thead>
                            <tr>
                                <th class="ocserv-users-id">ID</th>
                                <th class="ocserv-users-name">Username</th>
                                <th class="ocserv-users-remote-ip">Remote IP</th>
                                <th class="ocserv-users-ip">VPN IP</th>
                                <th class="ocserv-users-since">Since</th>
                                <th class="ocserv-users-state">State</th>
                            </tr>
                        </thead>
                        <tbody id="users-table-tbody">
                        </tbody>
                        </table>
                </div>
            </section>
        </main>
    </div>
<script>
var raw;
var status_raw;

occtl_users_run();
occtl_status_run();

function occtl_users_run() {
    var proc = cockpit.spawn(["/usr/bin/occtl", "-n", "-j", "show", "users"],
			     { err: "message", superuser: "try" });
    proc.done(occtl_users_success);
    proc.stream(occtl_users_output);
    proc.fail(occtl_users_fail);

    raw = "";
}

function occtl_users_success() {
    var obj;
    try {
	    obj = JSON.parse(raw);
    } catch(e) {
	    console.warn(e, obj);
    }

    $("#users-table-tbody").empty();

    $.each(obj, function (index,Object) {
	    var ipv4 = Object.IPv4;
	    var ipv6 = Object.IPv6;
	    var ip;

	    if (ipv4 && ipv6) {
	    ip = ipv4 + "/" + ipv6;
	} else {
	    if (ipv4)
		ip = ipv4;
	    else
		ip = ipv6;
	}

	var row = $("<tr>").append(
	    $('<td class="ocserv-users-id">').text(Object.ID),
	    $('<td class="ocserv-users-name">').text(Object.Username),
		$('<td class="ocserv-users-remote-ip">').text(Object["Remote IP"]),
		$('<td class="ocserv-users-ip">').text(ip),
		$('<td class="ocserv-users-since">').text(Object["Connected at"]),
		$('<td class="ocserv-users-state">').text(Object.State)
	);
	$("#users-table-tbody").append(row);
    });

    $('.curtain-ocserv').hide();
    $('body').show();
}

function occtl_users_fail(ex) {
    if (ex.problem == "not-found")
	console.warn("occtl executable was not found");
    else
	console.warn(cockpit.message(ex));
    $('.curtain-ocserv').show();
    $('body').show();
}

function occtl_users_output(data) {
    raw = raw.concat(data);
}

function occtl_status_run() {
    function occtl_status_output(data) {
	status_raw = status_raw.concat(data);
    }

    status_raw = "";

    var proc = cockpit.spawn(["/usr/bin/occtl", "-n", "-j", "show", "status"],
			     { err: "message", superuser: "try" });
    proc.done(occtl_status_success);
    proc.stream(occtl_status_output);
    proc.fail(occtl_status_fail);
}

function occtl_status_success() {
    var obj;
    try {
	    obj = JSON.parse(status_raw);
    } catch(e) {
	    console.warn(e, obj);
    }

    $("#status-table").empty();
    if (!obj) {
	occtl_status_fail();
    } else {
	$("#status-table").append('<tr><td>Status</td><td>' +
	    obj.Status + '</td></tr>');
	$("#status-table").append('<tr><td>Uptime</td><td>' +
	    obj["_Up since"] + '</td></tr>');
	$("#status-table").append('<tr><td>Connected users</td><td>' +
	    obj["Clients"] + '</td></tr>');
	$("#status-table").append('<tr><td>IPs in ban list</td><td>' +
	    obj["IPs in ban list"] + '</td></tr>');
    }
}

function occtl_status_fail(ex) {
    console.log("status fail", ex);
    var message = ex ? cockpit.message(ex) : "offline";
    $("#status-table")
	.empty()
	.append('<tr>')
	    .append($('<td>Status</td>'),
		    $('<td>').text(message));
}
</script>
</body>
</html>

